package com.irmasoft.additifsalimentaire;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	public ListView lv;
	public EditText searchWord;
	public ArrayList<HashMap<String,String>> listAdditifOrigine = new ArrayList<HashMap<String,String>>();
	public ArrayList<HashMap<String,String>>  listAdditifLessInfo  = new ArrayList<HashMap<String,String>>();
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        searchWord = (EditText) findViewById(R.id.editText);
        searchWord.setBackgroundColor(Color.CYAN);
        listAdditifOrigine.clear();
        parsingAdditifsSource();
        prepareAdditifInfoLess();
    }

    @Override
    protected void onStart() {
    	SimpleAdapter adapter = new SimpleAdapter(this, listAdditifLessInfo,
                R.layout.additif_layout,
                new String[] {"code", "name"},
                new int[] {R.id.text1, R.id.text2});
    	lv.setAdapter(adapter);
    	lv.setOnItemClickListener(new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int i, long l) {
				@SuppressWarnings("unchecked")
				HashMap<String,String> map = (HashMap<String, String>) lv.getItemAtPosition(i);
				String code = map.get("code");
				Intent intent = new Intent(getApplicationContext(), DisplayAdditif.class);
				intent.putExtra("code", code);
				String infos = new String();
				for(HashMap<String, String> mapOrigine : listAdditifOrigine){
					if(code.equals(mapOrigine.get("code"))){
						infos = mapOrigine.get("info");
					}
				}
				intent.putExtra("info", infos);
				startActivity(intent);
			}
        });
    	searchWord.addTextChangedListener(new TextWatcher() {
    		@Override
    	    public void afterTextChanged(Editable editable) {
    			filterAdditifsList(lv, searchWord.getText().toString());
    	    }

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {  }
			
			});
    	super.onStart();
    }
    
    private void filterAdditifsList(ListView lv, String word) {
    	SimpleAdapter adapter = new SimpleAdapter(this, populateListOnChangeSearchWord(listAdditifLessInfo, word), R.layout.additif_layout,
                new String[] {"code", "name"},
                new int[] {R.id.text1, R.id.text2});
    	lv.setAdapter(adapter);
	}
    
    public void parsingAdditifsSource(){
    	List<String[]> lAdditifs = new ArrayList<String[]>();
    	String[] lignes = SourceAdditifs.additifs.split("ENDLINE");
    	for(String additif : lignes){
    		lAdditifs.add(additif.split("="));
    	}
    	for(String[] additif : lAdditifs){
    		HashMap<String,String> map = new HashMap<String, String>();
    		map.put("code", additif[0]);
    		map.put("info",  additif[1]);
    		listAdditifOrigine.add(map);
    	}
    }

    private  void prepareAdditifInfoLess() {
    	for(HashMap<String,String> map : listAdditifOrigine){
    		HashMap<String,String> temp = new HashMap<String,String>();
        	temp.put("code", map.get("code"));
        	String name = map.get("info").split("NEXT")[0];
        	temp.put("name", name);
        	listAdditifLessInfo.add(temp);
    	}
    }
    
    private ArrayList<HashMap<String,String>>  populateListOnChangeSearchWord(ArrayList<HashMap<String,String>> listAdditifLessInfo, String word) {
    	ArrayList<HashMap<String,String>> listAdditif = new ArrayList<HashMap<String,String>>();
    	for(HashMap<String,String> map : listAdditifLessInfo){
    		if(((map.get("code")).toLowerCase()).startsWith(word.toLowerCase())){
    			listAdditif.add(map);	
    		}
    	}
    	if(listAdditif.isEmpty()){
    		searchWord.setBackgroundColor(Color.RED);
    	} else {
    		searchWord.setBackgroundColor(Color.CYAN);
    	}
    	return listAdditif;
    }
}
